package cn.echoedu.model;

import java.sql.SQLException;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.ClassNotify;
import cn.echoedu.entity.ReceiveClassNotify;
import cn.echoedu.entity.receivecommand.ClassNotifyFirstReceive;
import cn.echoedu.entity.receivecommand.MonitorAddNotifyReceive;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class ClassNotifyModel extends BaseModel{
	private final String T = "ClassNotifyModel-------->>";
	public int addSchoolNotifyContent(ClassNotify classnotify) {
		// TODO Auto-generated method stub
		return  putInfoGetPrimaryKey(classnotify);
	}

	private int putInfoGetPrimaryKey(ClassNotify classnotify) {
		// TODO Auto-generated method stub
		try{
			return getDAOInsert().putInfoGetPrimaryKey(getConnection(), 
					TableName.CLASS_NOTIFY_TABLE, 
					classnotify,true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}



	public List<ClassNotify> queryByReceiveClassNotifys(
			List<ReceiveClassNotify> receiveclassnotifylist) {
		// TODO Auto-generated method stub
		String sql = getReceiveClassNotifyQuerySql(receiveclassnotifylist);

		L.p(T+sql);
		return getPojoForSql(sql);
	}
	private List<ClassNotify> getPojoForSql(String sql){

		try {
			return getDAORead().getPojoForSql(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	private String getReceiveClassNotifyQuerySql(List<ReceiveClassNotify>
	receiveclassnotifylist){
		StringBuilder sqlstring = new StringBuilder();
		sqlstring.append("select * from classnotify where classnotifyid in");
		sqlstring.append(StringUtil.getBeforeTheBsrackets());
		for(int i=0,len=receiveclassnotifylist.size();i<len;i++){
			sqlstring.append(StringUtil.varToSqlObject(receiveclassnotifylist.
					get(i).getClassnotify().getClassnotify()));
			if(i<len-1){
				sqlstring.append(StringUtil.getComma());
			}
			L.p("�ɽ��ܵ�֪ͨid�б�"+receiveclassnotifylist.
					get(i).getClassnotify().getClassnotify());
		}
		sqlstring.append(StringUtil.getAfterTheBrackets());
		return sqlstring.toString();
	}

	public boolean deleteNotifyById(int classnotifyid) {
		// TODO Auto-generated method stub
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(classnotifyid);
		try {
			return getDAODelete().deleteFrom(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					classnotify);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public ClassNotify queryClassNotifyFirst(
			ClassNotifyFirstReceive classnotifyfirst) {
		// TODO Auto-generated method stub
		String sql  = getSql(classnotifyfirst);
		try {
			List<ClassNotify> list = getDAORead().getPojoForSql(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					sql);
			if(list!=null&&list.size()>0){
				return list.get(0);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}


	private String getSql(ClassNotifyFirstReceive classnotifyfirst) {
		// TODO Auto-generated method stub
		StringBuilder sqlstr = new StringBuilder();
		sqlstr.append(" select * from receiveclassnotify right join  classnotify" +
				" on (receiveclassnotify.classnotifyid=classnotify.classnotifyid)"+
				" where receiveclassnotifyid in("+
				" select max(receiveclassnotifyid) from receiveclassnotify where"+
				" (receiveusernumber = ");
		sqlstr.append(classnotifyfirst.getUser_number());
		sqlstr.append(" or receiveclasscode =");
		sqlstr.append(classnotifyfirst.getAclasscode());
		sqlstr.append(" and classnotifyisdefault = 1) or (receiveclasscode = ");
		sqlstr.append(classnotifyfirst.getAclasscode());
		sqlstr.append(" and  classnotifyisdefault = 0))");
		return sqlstr.toString();
	}

	public int getMaxIdOfNotify(MonitorAddNotifyReceive monitoraddnotifyreceive) {
		// TODO Auto-generated method stub
		int maxid = 0;
		try {
			maxid = getDAORead().getMaxId(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					"classnotifyid",null,null,"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxid;
	}

	public int getMaxIdOfNotifyOfAClass(
			MonitorAddNotifyReceive monitoraddnotifyreceive) {
		// TODO Auto-generated method stub
		int maxid = 0;
		try {
			maxid = getDAORead().getMaxId(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					"classnotifyid","classcode",
					monitoraddnotifyreceive.getAclasscode()+"",null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxid;
	}

	public boolean addClassNotifyKeyContent(ClassNotify initClassNotify) {
		// TODO Auto-generated method stub
		try{
			L.p(T+"����");
			return getDAOInsert().putInto(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					initClassNotify,false);
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateClassNotify(ClassNotify classnotify,
			ClassNotify classnotify2) {
		// TODO Auto-generated method stub

		try {
			return getDAOUpdate().updateInfo(getConnection(),
					TableName.CLASS_NOTIFY_TABLE, classnotify, classnotify2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	public int getCurrentMiniIdOfNotify(
			MonitorAddNotifyReceive monitoraddnotifyreceive) {
		// TODO Auto-generated method stub
		int maxid = 0;
		try {
			maxid = getDAORead().getMiniId(getConnection(), TableName.CLASS_NOTIFY_TABLE,
					"classnotifyid","classcode",
					monitoraddnotifyreceive.getAclasscode()+"",null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxid;
	}







}
